package cn.oms.oms.service.impl;

import cn.oms.oms.entity.Admission;
import cn.oms.oms.entity.Emr;
import cn.oms.oms.entity.Patient;
import cn.oms.oms.exception.ServiceException;
import cn.oms.oms.mapper.AdmissionMapper;
import cn.oms.oms.mapper.EmrMapper;
import cn.oms.oms.mapper.PatientMapper;
import cn.oms.oms.service.IEmrService;
import cn.oms.oms.utils.TimeTool;
import cn.oms.oms.vo.EMRVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author AllenChen曾晨
 * @since 2022-04-29
 */
@Service
public class EmrServiceImpl extends ServiceImpl<EmrMapper, Emr> implements IEmrService {

    @Autowired
    private AdmissionMapper admissionMapper;

    @Autowired
    private EmrMapper emrMapper;

    @Autowired
    private PatientMapper patientMapper;


    /**
     * 根据查询条件查询电子病历列表
     * @param emrVo
     * @return
     */
    @Override
    public PageInfo<Emr> selectEMRList(EMRVo emrVo) {
        QueryWrapper<Emr> query = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(emrVo.getPatientName())){
           query.like("patientName",emrVo.getPatientName());
        }

        query.eq("deleteFlag",0);
        PageHelper.startPage(emrVo.getPageNum(),emrVo.getPageSize());
        List<Emr> emrs = emrMapper.selectList(query);

        for (Emr emr : emrs){
            emr.setReturnCreateTime(TimeTool.dateFormatStringLong(emr.getCreateTime()));
            emr.setReturnPatientDob(TimeTool.dateFormatStringShort(emr.getPatientDob()));
            emr.setReturnAdmissionTime(TimeTool.dateFormatStringShort(emr.getPatientAdmissionId()));
            Integer maritalStatus = emr.getMaritalStatus();
            if (maritalStatus == 0){
                emr.setReturnMari("未婚");
            }
            if (maritalStatus == 1){
                emr.setReturnMari("已婚");
            }
            if (maritalStatus == 2){
                emr.setReturnMari("离偶");
            }
            if (maritalStatus == 3){
                emr.setReturnMari("离婚");
            }
            if (maritalStatus == 4){
                emr.setReturnMari("其他");
            }
            Integer patientSex = emr.getPatientSex();
            if (patientSex==0){
                emr.setReturnSexName("男");
            }
            if (patientSex==1){
                emr.setReturnSexName("女");
            }

        }

        return new PageInfo<>(emrs);
    }

    /**
     * 根据ID查询详情
     * @param emrVo
     * @return
     */
    @Override
    public Emr selectEMRDetail(EMRVo emrVo) {
        if (emrVo.getEmrId()==null||"".equals(emrVo.getEmrId())){
            throw new ServiceException("参数异常,EMRID不能为空");
        }
        QueryWrapper<Emr> query = new QueryWrapper<>();
        query.eq("emrId",emrVo.getEmrId());
        query.eq("deleteFlag",0);
        Emr emr = emrMapper.selectOne(query);

        return emr;
    }

    /**
     * 新增电子病历
     * @param emrVo
     * @return
     */
    @Override
    public String addEMR(EMRVo emrVo) {
        Emr emr = new Emr();

        if (emrVo.getAdmissionId()==null||"".equals(emrVo.getAdmissionId())){
            throw new ServiceException("参数异常，入院登记ID为空");
        }
        // 查入院登记
        QueryWrapper<Admission> query = new QueryWrapper<>();
        query.eq("admissionId",emrVo.getAdmissionId());
        query.eq("deleteFlag",0);
        Admission admission = admissionMapper.selectOne(query);
        if (admission==null){
            throw new ServiceException("参数异常，无法找到入院信息");
        }

        // 查患者信息
        Integer patientId = admission.getPatientId();
        QueryWrapper<Patient> query1 = new QueryWrapper<>();
        query1.eq("patientId",patientId);
        query1.eq("deleteFlag",0);
        Patient patient = patientMapper.selectOne(query1);
        if (patient==null){
            throw new ServiceException("参数异常，无法找到患者信息");
        }
        emr.setAdmissionId(emrVo.getAdmissionId());
        emr.setPatientAdmissionId(admission.getCreateTime());
        emr.setPatientId(patient.getPatientId());
        emr.setPatientName(patient.getPatientName());
        emr.setBlood(patient.getBlood());
        emr.setPatientDob(patient.getPatientDob());
        emr.setPatientAge(patient.getAge());
        emr.setPatientSex(patient.getSex());
        emr.setMaritalStatus(patient.getMaritalStatus());
        emr.setNationality(patient.getNationality());
        emr.setNativePlace(patient.getNativePlace());
        emr.setDocumentId(patient.getDocumentNo());
        emr.setHomeAddress(patient.getHomeAddress());
        emr.setNowAddress(patient.getNowAddress());
        emr.setPatientPhone(patient.getPhone());
        emr.setPatientEmail(patient.getEmail());
        emr.setPatientHeight(patient.getHeight());
        emr.setPatientWeight(patient.getWeight());

        // 过敏药物
        emr.setAllergyDrugName(emrVo.getAllergyDrugName());
        emr.setSymptomName(emrVo.getSymptomName());
        emr.setDiagnosisDetail(emrVo.getDiagnosisDetail());
        emr.setCreateTime(new Date());
        emr.setDeleteFlag(0);

        int num = emrMapper.insert(emr);

        if (num!=1){
            throw new ServiceException("数据库修改异常！！！");
        }

        return "ok";
    }

    /**
     * 修改电子病历
     * @param emrVo
     * @return
     */
    @Override
    public String updateEMR(EMRVo emrVo) {
        if (emrVo.getEmrId()==null||"".equals(emrVo.getEmrId())){
            throw new ServiceException("参数异常，EMRid为空");
        }
        QueryWrapper<Emr> query = new QueryWrapper<>();
        query.eq("emrId",emrVo.getEmrId());
        query.eq("deleteFlag",0);
        Emr emr = emrMapper.selectOne(query);
        if (emr==null){
            throw new ServiceException("参数异常，未查询到电子病历");
        }
        emr.setAllergyDrugName(emrVo.getAllergyDrugName());
        emr.setSymptomName(emrVo.getSymptomName());
        emr.setDiagnosisDetail(emrVo.getDiagnosisDetail());

        int num = emrMapper.updateById(emr);

        if (num!=1){
            throw new ServiceException("数据库修改异常！！！");
        }
        return "ok";
    }

    /**
     * 删除电子病历
     * @param id
     * @return
     */
    @Override
    public String deleteEMR(Integer id) {

        if (id==null||"".equals(id)){
            throw new ServiceException("参数异常，EMRID为空");
        }
        QueryWrapper<Emr> query = new QueryWrapper<>();
        query.eq("emrId",id);
        query.eq("deleteFlag",0);
        Emr emr = emrMapper.selectOne(query);
        if (emr==null){
            throw new ServiceException("参数异常，为查询到电子病历信息");
        }
        emr.setDeleteFlag(1);
        int num = emrMapper.updateById(emr);
        if (num!=1){
            throw new ServiceException("数据库修改异常！！！");
        }
        return "ok";
    }

}
